<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
</head>

<body>
    <div class="userinfo">
        <h3></h3>
        <p></p>
    </div>
    <h1>首页</h1>

    <!-- 购物车按钮 -->
    <button id="cart">购物车</button>
    <!-- 个人订单 -->
    <button id="orders">个人订单</button>
    <script>
        let userInfo = JSON.parse(localStorage.getItem("userinfo"));
        let h3 = document.querySelector(".userinfo > h3");
        h3.innerText = userInfo.nickname;

        let p = document.querySelector(".userinfo > p");
        p.innerText = userInfo.desc;

        // 购物车
        let cartBtn = document.querySelector("#cart");

        cartBtn.onclick = () => {
            const xhr = new XMLHttpRequest();
            xhr.onreadystatechange = () =>{
                if (xhr.status == 200 && xhr.readyState == 4) {
                    // 向ajax后台的程序发送xmlhttp请求的时候,
                    // 后台程序接到请求会进行处理,处理结束后,可以返回一串数据给前台
                    console.log("购物车加载：", xhr.responseText);
                }
            }
            xhr.open("GET", "/cart");
            // 配置请求头
            xhr.setRequestHeader(
                "content-type",
                "application/x-www-form-urlencoded",
            )
            xhr.send();
        }
    </script>
</body>

</html>